
using System.Collections;
using QdbUnit;
using Qdb;
using Qdb.Config;
using Qdb.Foundation;
using Qdb.Internal;
using Qdb.Internal.Freespace;
using Qdb.Internal.Slots;
using Qdb.Tests.Common.Api;
using Qdb.Tests.Common.Freespace;

namespace Qdb.Tests.Common.Freespace
{
	public class FreespaceManagerTypeChangeSlotCountTestCase : TestWithTempFile
	{
		private const int Size = 10000;

		private LocalObjectContainer _container;

		private IClosure4 _currentConfig;

		public static void Main(string[] args)
		{
			new ConsoleTestRunner(typeof(FreespaceManagerTypeChangeSlotCountTestCase)).Run();
		}

		/// <exception cref="System.Exception"></exception>
		public virtual void TestMigrateFromRamToBTree()
		{
			CreateDatabaseUsingRamManager();
			MigrateToBTree();
			Reopen();
			CreateFreeSpace();
			IList initialSlots = GetSlots(_container.FreespaceManager());
			Reopen();
			IList currentSlots = GetSlots(_container.FreespaceManager());
			Assert.AreEqual(initialSlots, currentSlots);
			_container.Close();
		}

		/// <exception cref="System.Exception"></exception>
		public virtual void TestMigrateFromBTreeToRam()
		{
			CreateDatabaseUsingBTreeManager();
			MigrateToRam();
			CreateFreeSpace();
			IList initialSlots = GetSlots(_container.FreespaceManager());
			Reopen();
			Assert.AreEqual(initialSlots, GetSlots(_container.FreespaceManager()));
			_container.Close();
		}

		private void Reopen()
		{
			_container.Close();
			Open();
		}

		private void CreateDatabaseUsingRamManager()
		{
			ConfigureRamManager();
			Open();
		}

		private void CreateDatabaseUsingBTreeManager()
		{
			ConfigureBTreeManager();
			Open();
		}

		private void Open()
		{
			_container = (LocalObjectContainer)QdbFactory.OpenFile(((IConfiguration)_currentConfig
				.Run()), TempFile());
		}

		private void CreateFreeSpace()
		{
			Slot slot = _container.GetSlot(Size);
			_container.Free(slot);
		}

		/// <exception cref="System.Exception"></exception>
		private void MigrateToBTree()
		{
			_container.Close();
			ConfigureBTreeManager();
			Open();
		}

		private void ConfigureBTreeManager()
		{
			_currentConfig = new _IClosure4_83();
		}

		private sealed class _IClosure4_83 : IClosure4
		{
			public _IClosure4_83()
			{
			}

			public object Run()
			{
				IConfiguration config = QdbFactory.NewConfiguration();
				config.Freespace().UseBTreeSystem();
				return config;
			}
		}

		/// <exception cref="System.Exception"></exception>
		private void MigrateToRam()
		{
			_container.Close();
			ConfigureRamManager();
			Open();
		}

		private void ConfigureRamManager()
		{
			_currentConfig = new _IClosure4_98();
		}

		private sealed class _IClosure4_98 : IClosure4
		{
			public _IClosure4_98()
			{
			}

			public object Run()
			{
				IConfiguration config = QdbFactory.NewConfiguration();
				config.Freespace().UseRamSystem();
				return config;
			}
		}

		private IList GetSlots(IFreespaceManager freespaceManager)
		{
			IList retVal = new ArrayList();
			freespaceManager.Traverse(new _IVisitor4_107(retVal));
			return retVal;
		}

		private sealed class _IVisitor4_107 : IVisitor4
		{
			public _IVisitor4_107(IList retVal)
			{
				this.retVal = retVal;
			}

			public void Visit(object obj)
			{
				retVal.Add(obj);
			}

			private readonly IList retVal;
		}
	}
}
